package com.gopay.bis.manager.bank.ebank;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;

import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.common.Result;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.boc.ebank.BocB2bMessage;
import com.gopay.common.exception.GopayException;
import com.gopay.remote.bank.BankManagerRemoteService;
import com.gopay.remote.bank.businesses.BankService;

/**
 * @BocB2bManager.java
 * @author wanggang
 * @2013-11-4 下午2:35:29  www.gopay.com.cn Inc.All rights reserved.
 */
public class BocB2bManager implements BankManagerRemoteService{

    @Resource(name = "bocConfig")
    private Map<String, String> bocConfig;
    
    @Resource(name = "bocB2bBankServiceClient")
    private BankService bocB2bBankServiceClient;
	

	
	/**
	 * B2B签名
	 * 
	 * @param message
	 * @return
	 */
	@Override
	public Object sign(BaseMessage baseMessage) throws GopayException {
		// 获取银行接口通道配置信息
		BocB2bMessage message = (BocB2bMessage) baseMessage;
		// 交易金额保留小数点后两位(中行要求)
        DecimalFormat df = new DecimalFormat("0.00");
        String amount = df.format(message.getAmount());
        message.setAmount(new BigDecimal(amount));
        message.setCurCode((String) bocConfig.get("BOC.CURTYPE.CNY"));
        message.setOrderUrl(message.getPayRuturnUrl());
        message.setMerchantNo(message.getMerID());
        message.setBankUrl(message.getPayUrl());
        message.setOrderNote("gopay");
        if (StringUtils.isNotBlank(message.getPaymentAccInfo())) {
            message.setOrderNote(message.getPaymentAccInfo());
        }
        
        message.setPlainMessage(buildPlain(message));
    	BankLogUtils.printB2BManagerSignSendLog(message);
        String signStr = (String) bocB2bBankServiceClient.sign(message);
        message.setSignMessage(signStr);
    	BankLogUtils.printB2BManagerSignReturnLog(message);
    	
        return message;
	}
	
	/**
	 * B2B验签
	 * 
	 * @param message
	 * @return
	 */
	@Override
	public Object verify(BaseMessage baseMessage) throws GopayException {
		BocB2bMessage message = (BocB2bMessage) baseMessage;
		StringBuffer sb = new StringBuffer();
        sb.append(message.getTranSeq()).append("|");
        sb.append(message.getTranCode()).append("|");
        sb.append(message.getTranStatus()).append("|");
        sb.append(message.getTranTime()).append("|");
        sb.append(message.getAmount().toString()).append("|");
        sb.append(message.getFeeAmount()).append("|");
        sb.append(message.getBocNo()).append("|");
        sb.append(message.getOrderId()).append("|");
        sb.append(message.getCurCode());
        message.setPlainMessage(sb.toString());
    	BankLogUtils.printB2BManagerVerifySendLog(message);
        boolean flag = (Boolean) bocB2bBankServiceClient.verify(message);
    	BankLogUtils.printB2BManagerVerifyReturnLog(message, flag);

        return flag;
	}
	
	/**
	 * B2B订单查询
	 * 
	 * @param message
	 * @return
	 */
	@Override
	public Object query(Object obj) throws GopayException {
		BocB2bMessage message = (BocB2bMessage) obj;
		message.setMerchantNo(message.getMerID());
		message.setBankUrl(message.getQueryUrl());
    	BankLogUtils.printB2BManagerQuerySendLog(message, "");
		message = (BocB2bMessage) bocB2bBankServiceClient.query(message);
    	BankLogUtils.printB2BManagerQueryReturnLog(message);
		return message;
	}
    

    /**
     * B2B签名明文
     * 
     * @param message
     * @return
     */
    private String buildPlain(BocB2bMessage message) {
        StringBuffer sb = new StringBuffer("");

        sb.append(StringUtils.trimToEmpty(message.getMerchantNo())).append("|");
        sb.append(StringUtils.trimToEmpty(message.getOrderId())).append("|");
        sb.append(StringUtils.trimToEmpty(message.getCurCode())).append("|");
        if (message.getAmount() != null) {
            sb.append(StringUtils.trimToEmpty(message.getAmount().toString()));
        }
        sb.append("|");
        sb.append(StringUtils.trimToEmpty(message.getOrderDate()));

        return sb.toString();
    }

	@Override
	public Object dayend(BaseMessage base) throws GopayException, Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object refund(BaseMessage base) throws GopayException {
		// TODO Auto-generated method stub
		return null;
	}
}
